import { getModelList } from "@/api";
import { getSearchFilter } from "@/common/function";
import storage from "@/common/function/storage";
import QQMapWX from "@/utils/qqmap-wx-jssdk.min.js";

// // 实例化API核心类
let qqmapsdk = new QQMapWX({
  key: "2DPBZ-DPTKW-VSXR5-YLS7O-DY2A5-ROF6P", // 必填
});

const state = {
  city: storage.get("City") || "长沙",
  orderAddress: "",
  currentAddress: {},
};

const mutations = {
  GET_CURRENT_ADDRESS(state, data) {
    state.currentAddress = data.list[0];
  },
};

const actions = {
  getCurrentCity() {
    try {
      uni.getLocation({
        type: "gcj02",
        geocode: true,
        success: function (res) {
          //逆地址解析  坐标转地址信息
          qqmapsdk.reverseGeocoder({
            //Object格式
            location: {
              latitude: res.latitude,
              longitude: res.longitude,
            },
            success: function (res) {
              //成功后的回调
              const mapdata = res.result.ad_info;
              storage.set("City", mapdata.city);
              // console.log("地址", mapdata);
            },
            fail: function (error) {
              console.error(error);
            },
            complete: function (res) {
              //console.log(res);
            },
          });
        },
      });
    } catch (error) {
      console.log(error);
    }
  },

  // 获取当前默认位置
  async getCurrentLocationData({ commit }, { creatorUserId }) {
    let sortData = {
      currentPage: 1,
      pageSize: -1,
      filter: getSearchFilter({ creatorUserId }, { method: "eq" }),
      sort: {
        isDefault: "desc",
      },
    };
    const res = await getModelList("65c1d9085efd5153ff55e6f4", sortData);
    // console.log("res", res);
    if (res.code === 200) {
      commit("GET_CURRENT_ADDRESS", res?.data);
    }
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
